package com.atguigu.lease.web.admin.schdule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.util.Date;

/**
 * @author Ahmad
 * @date 2024/12/25 11:12
 * @description
 *
 * 定时任务类
 */
@Component
public class ScheduledTasks {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    /**
     * 此方法用于检查租约是否过期，指定扫描的时间，该方法会自动调用
     */
    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus(){
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper =new LambdaUpdateWrapper<>();
        Date current = new Date();
        updateWrapper.eq(LeaseAgreement::getLeaseEndDate, current);//确认比较规则，当前时间和租约结束时间
        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING);//确认租约的类型
        updateWrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);//如果过期了就变成已过期
        leaseAgreementService.update(updateWrapper);
    }




}